/**
 * Created by Administrator on 2015/9/18.
 */
jQuery(function($) {
    var doc = document,
        doc_ = $(doc); //将document对像缓存以保证性能

    doc_.on("click", ".head li a", function(event) {
        var link = $(this),
            links = $(".head li a");

        $.each(links, function(i, item) {
            links.eq(i).removeClass("active");
        });
        link.addClass("active");
    });

    //兼容安桌和IOS


    //以下两个事件为显示表单
    //ios事件处理
    doc_.on("touchstart", ".nav_list .list", function(event) {
        event.preventDefault();	//阻止冒泡到click事件
        $(".nav_list .search_info").addClass("test");
        $(".nav_list .search_info input").addClass("stretching");

        $(".stretching").focus();
    });

    doc_.on("click", ".nav_list .list", function(event) {
        $(".nav_list .search_info").addClass("test");
        $(".nav_list .search_info input").addClass("stretching");

        $(".stretching").focus();
    });

    //ios事件处理
    doc_.on("touchstart", ".nav_list .proList", function(event) {
        event.preventDefault();	//阻止冒泡到click事件
        $(".nav_list .search_info input").addClass("stretching");

        $(".stretching").focus();
    });

    doc_.on("click", ".nav_list .proList", function(event) {
        $(".nav_list .search_info input").addClass("stretching");

        $(".stretching").focus();
    });

    //当不符合表单出现条件则关闭表单
    doc_.on("touchstart", function(event) {

        var target = event.target;
        if (target.className !== "list" && target.className !== "proList" &&
            target.tagName !== "INPUT") {
            $(".nav_list .search_info input").removeClass("stretching");
        }
        //event.preventDefault();
    });

    doc_.on("click", function(event) {
        var target = event.target;
        if (target.className !== "list" && target.className !== "proList" &&
            target.tagName !== "INPUT" ) {
            $(".nav_list .search_info input").removeClass("stretching");
        }
    });

    //删除class=proList的列表按钮(单独删除)
    doc_.on("touchstart", ".proList ", function(event) {
        //console.log();
        var elm = $(this).parent();
        elm.addClass("fadeout");
        setTimeout(function() {
            elm.remove();
        }, 500);
    });

    doc_.on("click", ".proList .close", function(event) {
        //console.log();
        var elm = $(this).parent();
        elm.addClass("fadeout");
        setTimeout(function() {
            elm.remove();
        }, 500);
    });

    //删除class=proList的列表按钮(全部删除)
    doc_.on("touchstart", ".proList .clear", function(event) {
        var elms = $(".proList p");
        $.each(elms, function(i, item) {
            elms.eq(i).addClass("fadeout");
        });

        //0.5秒后移除元素
        setTimeout(function() {
            $.each(elms, function(i, item) {
                elms.eq(i).remove();
            });
        }, 500);
    });

    doc_.on("click", ".proList .clear", function(event) {
        var elms = $(".proList p");
        $.each(elms, function(i, item) {
            elms.eq(i).addClass("fadeout");
        });

        //0.5秒后移除元素
        setTimeout(function() {
            $.each(elms, function(i, item) {
                elms.eq(i).remove();
            });
        }, 500);
    });

    //获取class="stretching"表单值，并将其加入到class="proList"中
    doc_.on("keyup", function(event) {

        var elm = $(".stretching"),
            val = elm.val();
        console.log(val === undefined);
        if (event.which === 13 && val !== undefined) {
            var proList = $(".proList");
            proList.append("<p>" + val + "<em class='close'></em></p>");
            elm.val("");
        }
    });

    //将filter-row值添加到class=proList中
    doc_.on("click", ".filter-row a", function(event) {
        var target = event.target;
        if (target.tagName === "A") {
            var string = target.innerHTML;

            //用于判断filter值是否已加入到proList中，如果未加入则index = 1
            //否则不将值加入到proList中
            if (target.index !== 1) {

                target.index = 1;
                var text = target.innerHTML;
                var proList = $(".proList");
                proList.append("<p>" + text + "<em class='close'></em></p>");
                return;
            }

        }

    });


    jQuery.ajaxSetup({
        case:false,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    //如果删除了filter-row中的值,则将index恢得到初始状态
    doc_.on("click", ".proList .close", function(event) {
        console.log();
        if (event.target.parentNode.index === 1) {
            event.target.parentNode.index = 0;
        }

    });

    //切换filter-row的值
    function setIndex() {
        var links = doc.querySelectorAll(".row nav a"),
            len = links.length;

        for (var i = 0;i < len; i++) {
            links[i].index = i;
        }
        //console.log(links[2].index);
    }
    setIndex();

    //切换tab标签
    doc_.on("click", ".row a", function(event) {
        var rows = $(".row ul"),
            rowelm = $(".filter-row"),
            links = $(".row nav a"),
            tabs = $(".filter-row ul"),
            index = event.target.index;

        $.each(tabs, function(i, item) {
            tabs.eq(i).addClass("hide");
            tabs.eq(i).removeClass("show");
            links.eq(i).removeClass("selected");
        });

        rowelm.addClass("show");
        tabs.eq(index).addClass("show");
        links.eq(index).addClass("selected");
        //console.log(links.length);
    });

    var obj={
        keyword:'',
        stage  :'',
        phase  :'',
        field  :'',
        dist   :''
    };

    doc_.on("click","a[name=stage]",function(){
        var stageId = $(this).attr('id');
        obj.stage = stageId;
        searchProjectByTag()
    });

    doc_.on("click","a[name=phase]",function(){
        var phaseId = $(this).attr('id');
        obj.phase = phaseId;
        searchProjectByTag()
    });

    doc_.on("click","a[name=field]",function(){
        var fieldId = $(this).attr('id');
        obj.field = fieldId;
        searchProjectByTag()
    });

    doc_.on("click","a[name=district]",function(){
        var distId = $(this).attr('dist_name');
        obj.dist = distId;
        searchProjectByTag()
    });

    function searchProjectByTag() {
        var getUrl = "?keyword=" + obj.keyword + "&stage=" + obj.stage + "&phase=" + obj.phase + "&field=" + obj.field + "&dist=" + obj.dist;
        History.pushState(null, null, getUrl);
        $("#project_list").html("<li style='text-align: center;padding: 40px 0'><img src='/images/loading.gif' /></li>");
        $.ajax({
            type: 'get',
            url: searchProject + getUrl,
            dataType: 'json',
            success: function (obj) {
                $('#project_list').html('');
                setTimeout(function(){
                    if (obj.level == "success") {
                        showProjectBySearch(obj.data);
                    }
                },300);
            },
            error: function (obj) {
                var datajson = obj.responseJSON;
                noty({text: datajson.message, type: datajson.level, timeout: 600});
            }
        });
    }

    function showProjectBySearch(projects) {
        var HtmlAll='';
        for (var i = 0; i < projects.length; i++) {
            if (projects[i] !== 'undefined') {
                var HTMLInitData = '<li><dl><dt><a href="' + projects[i].id + '"><img src="' + showLogo + projects[i].id + '"/></a></dt>' +
                    '<dd class="commpany">' +
                    '<p><a href="' + projectUrl + projects[i]['id'] + '"><strong>' + projects[i]['project_name'] + '</strong></a></p>' +
                    '<p>' + projects[i]['project_intro'] + '</p>' +
                    '</dd>' +
                    '<dd class="list"><div class="list_content"><p class="stage"><em class="reveal">融资阶段</em>'+
                    '<em class="floatR">' + projects[i]['phase']['phase'] + '</em></p>'+
                    '<p class="type"><em class="reveal">所在行业</em>';
                    var fields = '';
                    for(var m in projects[i]['fields']) {
                       fields = fields +  '<em class="floatR">' + projects[i]['fields'][m].field + '</em>';
                    }
                 var district =   '</p>'+
                     '<p class="address"><em class="reveal">所在地区</em>'+
                     '<em class="floatR">' + projects[i]['province'] + projects[i]['city'] + '</em></p><p class="member">';

                var teams = '';
                for(var n in projects[i]['teams']){
                    teams = teams + '<em class="reveal">' + projects[i]['teams'][n].real_name + '</em>';
                }

                  var footer =  '</p></div></dd></dl><li>';
                 HTMLInitData = HTMLInitData + fields + district + teams + footer;

            }
            HtmlAll += HTMLInitData;
        }
        $("#project_list").html(HtmlAll);
    }
});